package com.tucc.hello.leetcode.no1_400.no201_400.no206;

import com.tucc.hello.leetcode.common.ListNode;

import java.util.List;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode listNode = ListNode.of(1, 2, 3);
        ListNode newListNode = solution.reverseList2(listNode);
        ListNode.print(newListNode);
    }

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        ListNode newHead = reverseList(cur);
        cur.next = head;
        head.next = null;
        return newHead;
    }

    public ListNode reverseList2(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}